//
//  KKQuestionCell.m
//  DoMi
//
//  Created by 康凯 on 2016/11/23.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKQuestionCell.h"
#import "KKQuestionUsersM.h"


@implementation KKQuestionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)initData:(KKQuestionListM *)data{
    [self.avatar sd_setImageWithURL:[NSURL URLWithString:data.avatar] placeholderImage:[UIImage imageNamed:@"default_avater"]];
    if([KKAppUtils isEmptyString:data.nickname]){
        self.nickname.text=@"匿名";
    }else{
        self.nickname.text=data.nickname;
    }
    float width=[KKAppUtils sizeWithText:data.job font:[UIFont systemFontOfSize:12] maxSize:CGSizeMake(100, 14)].width;
    self.jobWidth.constant=width+5;
    self.job.text=data.job;
    self.label.text=data.label;
    self.time.text=data.systime;
    self.title.text=data.title;
    if(data.content!=nil&&data.content.count>0){
        NSDictionary *con=data.content[0];
        self.content.text=[con objectForKey:@"content"];
    }else{
        self.content.text=@"";
    }
    
    self.money.text=[NSString stringWithFormat:@"%.1f元",[data.money floatValue]];
    if([data.money isEqualToString:@"0.00"]){
        self.overTime.text=@"";
    }else{
//        0未开始 1已开始 2 已结束
        if(data.status==0){
            self.overTime.text=@"悬赏尚未开始";
        }else if(data.status==1){
            NSString *time=[KKAppUtils timeformatFromSeconds:[data.lefttime intValue]];
            self.overTime.text=[NSString stringWithFormat:@"距离结束还有%@",time];
        }else{
            self.overTime.text=@"悬赏已经结束";
        }
    }
    
    self.questionCount.text=[NSString stringWithFormat:@"%@人回答",data.answercoun];
    self.browserCount.text=[NSString stringWithFormat:@"%@次浏览",data.browser];
    
    NSArray *users=data.users;
    for (UIView *subView in self.questionUserView.subviews) {
        [subView removeFromSuperview];
    }
    if(users!=nil&&users.count>0){
        CGFloat perWidth=(screenWidth-20-20)/5;
        self.imgHeight.constant=60;
        self.containerHeight.constant=115;
        for(int i=0;i<users.count;i++){
            if(i<5){
                KKQuestionUsersM *user=users[i];
                CGFloat tempWidth;
                if(i!=0){
                    tempWidth=perWidth+5;
                }else{
                    tempWidth=perWidth;
                }
                UIImageView *userImg=[[UIImageView alloc] initWithFrame:CGRectMake(i*tempWidth,0, perWidth-5, perWidth-5)];
                [userImg sd_setImageWithURL:[NSURL URLWithString:user.avatar]];
                
                UIImageView *rank=[[UIImageView alloc] initWithFrame:CGRectMake(i*tempWidth+perWidth-35, 0, 30, 15)];
                [rank setImage:[UIImage imageNamed:[NSString stringWithFormat:@"rank%d",i+1]]];
                [self.questionUserView addSubview:userImg];
                [self.questionUserView addSubview:rank];
            }
        }
    }else{
        self.imgHeight.constant=0;
        self.containerHeight.constant=45;
    }
    
}
@end
